import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'

// 定义路由
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    // 使用懒加载
    component: () => import('../components/login/Login.vue')
  },
  {
    path: '/tr',
    name: 'TravelReservation',
    // 使用懒加载
    component: () => import('../components/TravelReservation/TravelReservationApp.vue'),
    meta: { requiresAuth: true }
  },
  {

    path: '/route',
    name: 'ShowRoute',
    component: ()=> import('../components/TravelRoute/Route.vue')
  }
]

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫 - 检查认证状态
router.beforeEach((to, from, next) => {
  // 导入Pinia store
  import('../stores/authStore.ts').then(({ useAuthStore }) => {
    const authStore = useAuthStore()

    if (to.meta.requiresAuth) {
      if (authStore.isLoggedIn) {
        next()
      } else {
        next({ name: 'Login' })
      }
    } else {
      next()
    }
  })
})

export default router